<?php
	class Controller_Pixelmanager extends Controller_Abstract
	{
		function actionIndex()
		{			
			ini_set('memory_limit', '512M');
			set_time_limit(600);
			
			$pmodel = new PixelManager();
			$now = time();
			$today_7 = mktime(0, 0, 0, date('m',$now),date('d',$now),date('Y',$now)) +  (Q::ini('appini/pixelmanager_time')*60*60);
			if($now >= $today_7){
				$this->_view['date'] = date('m/d/Y',mktime(date('h'), date('i'), date('s'), date('m') , date('d')-1, date('Y')));
			}else{
				$this->_view['date'] = date('m/d/Y',mktime(date('h'), date('i'), date('s'), date('m') , date('d')-2, date('Y'))); 
			}
			$this->_view['pixels'] = $pmodel->getPixelManager();
		}
		
		function actionInsert()
		{
			if(isset($this->_context->peak)){
				$cam = $this->_context->campaign;
				$pub = $this->_context->pub;
				$pla = $this->_context->pla;
				$peak = $this->_context->peak;							
				if(is_numeric($peak) && $peak<=100 && $peak!=""){
					$pmodel = new PixelManager();
					$pmodel->add($cam, $pub, $pla, $peak);
					if($this->_context->isAJAX()){
						return 'OK';
					}else{
						$infos = array();
						$infos[] = Util::getMessage(16001);
						$_SESSION['infos'] = $infos;
						return $this->_redirect(url('pixelManager/tblindex'));
					}
				}
			}
		}
		
		function actionSetpeak()
		{
			if(isset($this->_context->peak)){
				$peak = $this->_context->peak;
				$id = $this->_context->id;
				
				if(is_numeric($peak) && $peak<=100 && $peak!=""){
					$pmodel = new PixelManager();
					$pmodel->modifyPeak($id, $peak);
					if($this->_context->isAJAX()){
						return 'OK';
					}else{
						return $this->_redirect(url('pixelmanager/index'));
					}
				}
			}
		}
		
		function actionTblindex()
		{
			$pmodel = new PixelManager();
			$field = $this->_context->get('field', '');
			$condition = $this->_context->get('condition', '');
			$field_condition = array();
			if( $field != '' && $condition != '' ){
				$field_condition[$field] = $condition;
			}
			
			$this->_view['tblPixels'] = $pmodel->tblGetAll( $field_condition );
			$this->_view['field'] = $field;
			$this->_view['condition'] = $condition;
			if(isset($_SESSION['infos'])){
				$this->_view['infos'] = $_SESSION['infos'];
				unset($_SESSION['infos']);
			}
		}
		
		function actionTbldelete()
		{
			$pmodel = new PixelManager();
			$pmodel->tblDeleteById($this->_context->id);
			$infos = array();
			$infos[] = Util::getMessage(16002);
			$_SESSION['infos'] = $infos;
			return $this->_redirect(url('pixelManager/tblindex'));
		}
		
		public function actionTblupdate() 
		{
			$pmodel = new PixelManager();
			if ($this->_context->isGET() && isset($this->_context->id)) {
				$this->_view['tblPixel'] = $pmodel->tblGetById($this->_context->id);
			} else {
			$cam = $this->_context->campaign;
				$id = $this->_context->id;
				$pub = $this->_context->pub;
				$pla = $this->_context->pla;
				$peak = $this->_context->peak;							
				if(is_numeric($peak) && $peak<=100 && $peak!=""){
					$pmodel = new PixelManager();
					$pmodel->tblUpdateById($cam, $pub, $pla, $peak, $id);
					$infos = array();
					$infos[] = Util::getMessage(16003);
					$_SESSION['infos'] = $infos;
					return $this->_redirect(url('pixelManager/tblindex'));
				}
			}
		}
		
		
		
	}
?>